﻿
using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Ni_GameButton
{
    public class GameButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
    {
        public bool isActivity = true;
        public bool isToggle = false;
        public bool isSelect = false;

        [SerializeField] private PlayAniEnum AniType;
        [SerializeField] private Transform ClickTran;
        [SerializeField] int ClickSize = 80;
        [SerializeField] Animator ClickAnimator;
        [SerializeField] GameObject ClickSelect;
        [SerializeField] GameObject ClickUnSelect;

        [SerializeField] string 点击音效 = "";

        // public Color ClickColor = new Color(1,1,1,125);
        private Vector3 preSize;

        private Image Image;
        [SerializeField] private float 透明度 = 1f;
        private Color origeColor;

        public void SetImg(Sprite s)
        {
            Image.sprite = s;
        }


        [SerializeField] private UnityEvent OnClickEvent = new UnityEvent();
        private UnityEvent onClickEvent非配置 = new UnityEvent();

        public UnityEvent OnClickEvent非配置
        {
            get { return onClickEvent非配置; }
        }

        public void OnClick()
        {
            if (string.IsNullOrEmpty(点击音效))
            {
                点击音效 = "点击1";
            }

            onClickEvent非配置?.Invoke();
            OnClickEvent?.Invoke();
        }

        protected virtual void Awake()
        {
            Image = this.GetComponent<Image>();
            if (透明度 == 0)
            {
                透明度 = 1f;
            }

            if (ClickTran == null)
            {
                ClickTran = this.transform;
            }

            preSize = ClickTran.localScale;
            isClickDown = false;
        }



        public void OnPointerClick(PointerEventData eventData)
        {

            if (!isActivity)
            {
                return;
            }

            OnClick();
        }

        public void SetToggleState(bool isSelect)
        {
            if (isSelect)
            {
                playAni1();
                this.isSelect = true;
            }

            if (!isSelect)
            {
                playAni2();
                this.isSelect = false;
            }
        }


        // public void 模拟点击()
        // {
        //     if (AniType == PlayAniEnum.Click1)
        //     {
        //         isClickDown = true;
        //         
        //         var ani = transform.DOScale(preSize * ClickSize / 100f, 0.1f);
        //         ani.OnComplete(() => { playAni2(); });
        //     }
        //
        //     OnClick();
        // }

        private bool isClickDown;

        async void Click1Ani1(Action<bool> doWhat=null)
        {
            float t = 0f;
            Vector3 target = preSize * ClickSize / 100f;
            float dur = 0.1f;
            
            while (t<dur)
            {
                if (!isClickDown)
                {
                    doWhat?.Invoke(false);
                    return;
                }
                
                
                t += Time.deltaTime;
                var l = Vector3.Lerp(preSize, target, t / dur);
                ClickTran.localScale = l;
                await Task.Yield();
            }
            
            ClickTran.localScale = target;
            doWhat?.Invoke(true);
        }
        async void Click1Ani2(Action<bool> doWhat=null)
        {
            float t = 0f;
            Vector3 target = preSize * ClickSize / 100f;
            float dur = 0.1f;
            
            while (t<dur)
            {
                if (isClickDown)
                {
                    doWhat?.Invoke(false);
                    return;
                }
                
                
                t += Time.deltaTime;
                var l = Vector3.Lerp(target, preSize, t / dur);
                ClickTran.localScale = l;
                await Task.Yield();
            }
            
            ClickTran.localScale = preSize;
            doWhat?.Invoke(true);
        }
        void playAni1()
        {
            switch (AniType)
            {
                case PlayAniEnum.Click1:
                    Click1Ani1();
                    break;
                case PlayAniEnum.Animation:
                    ClickAnimator.Play("ClickDown");
                    break;
                case PlayAniEnum.TwoState:
                    ClickSelect.SetActive(true);
                    ClickUnSelect.SetActive(false);
                    break;
            }
        }

        void playAni2()
        {
            switch (AniType)
            {
                case PlayAniEnum.Click1:
                    Click1Ani2();
                    break;
                case PlayAniEnum.Animation:
                    ClickAnimator.Play("ClickUp");
                    break;
                case PlayAniEnum.TwoState:
                    ClickSelect.SetActive(false);
                    ClickUnSelect.SetActive(true);
                    break;
            }
        }

        /// <summary>
        /// Click Down up
        /// </summary>
        /// <param name="eventData"></param>
        public void OnPointerDown(PointerEventData eventData)
        {
            if (!isActivity)
            {
                return;
            }

            isClickDown = true;

            if (!isToggle)
            {
                playAni1();
            }


        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (!isActivity)
            {
                return;
            }
            isClickDown = false;

            if (!isToggle)
            {
                playAni2();
            }
        }

        public enum PlayAniEnum
        {
            Null,
            Click1,
            Animation,
            TwoState,
            // GameButtonTwoState,
        }
        
        // private enum ClickState
        // {
        //     Null,
        //     Down,
        //     Up
        // }
    }


}
